import nattyFetch from 'natty-fetch';
import Utils from "../utils/index";

const {API_URL, MOCK_URL} = require(`./../config/${ENV}.json`);
const context = nattyFetch.context({
  mockUrlPrefix: MOCK_URL,
  urlPrefix: API_URL,
  mock: false,
  withCredentials: false,
  traditional: true,
  data: {
    _wx_token_: '',
  },
  timeout: 15000,
  // 请按照需要开启
  fit: function (response) {
    let ret = {
      success: response.success
    };
    if (ret.success) {
      ret.content = response.data;
    } else {
      ret.error = {
        message: response.msg
      }
    }
    return ret;
  }
});

context.create(
  'WechatAPI', {

    //获取配置
    auth: {
      url: 'wechat/api/sign',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
    },

    //获取用户信息
    getUser: {
      mock: false,
      url: 'wechat/get/user',
      method: 'GET',
      storage: true
    },

    userIsBinding: {
      url: 'wechat/user/isBinding',
      method: 'GET',
      // 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response.success
        };
        if (ret.success) {
          ret.content = '已绑定';
        } else {
          ret.error = {
            message: response
          }
        }

        return ret;
      }
    },

    //验证用户信息
    checkUserCertNum: {
      url: 'yz/checkCert',
      method: 'GET'
    },

    //身份证号码验证业主信息
    checkUserCard: {
      url: 'yz/checkCard',
      method: 'GET'
    },

    //用户ID获取业主
    loadDoorplateById: {
      url: 'mp/loadDoorplateById',
      method: 'GET'
    },

    getInfoByOpenId: {
      url: 'mp/getinfoByOpenId',
      method: 'GET',
      storage: false
    },

    //用户ID获取绑定小区
    loadDoorBind: {
      url: 'mp/loadDoorBind',
        method: 'GET'
    },

    //用户ID获取小区
    loadCourtById: {
      url: 'mp/loadCourtById',
      method: 'GET'
    },

    // 获取所有小区
    getCourtAll: {
      url: 'xq/findAllPage',
      method: 'GET'
    },

    getCourtById: {
      url: 'xq/findOne',
      method: 'GET'
    },

    //用户电话号码获取小区
    findCourtByPhone: {
      url: 'mp/findPhonenByCourt',
      method: 'GET'
    },

    //小区ID获取当前任务
    findRenWuActive: {
      url: 'rw/findRenWuActive',
      method: 'GET'
    },

    //绑定业主
    bindDoor: {
      url: 'mp/doorbind',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      // 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response === 'success'
        };
        if (ret.success) {
          ret.content = '绑定成功';
        } else {
          ret.error = {
            message: response
          }
        }

        return ret;
      }
    },

    //参与
    bindCastok: {
      url: 'mp/doorCastok',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      // 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response.success
        };
        if (ret.success) {
          ret.content = '请求成功';
        } else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }
    },

    //拒绝
    bindCastno: {
      url: 'mp/doorCastno',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      // 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response.success
        };
        if (ret.success) {
          ret.content = '请求成功';
        } else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }
    },

    getProprietorByOpenId: {
      url: 'mp/getProprietorByOpenId',
      method: 'GET',
      storage: false
    },

    //绑定用户信息
    binding: {
      url: 'wechat/user/binding',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
    },

    // 獲取投票列表()
    getVoteListByProprietorId: {
      url: 'fatp/findPlanByProprietor',
      method: 'GET'
    },

    // 獲取投票方案信息詳情
    getVoteById: {
      url: 'fatp/findPlanInProcess',
      method: 'GET'
    },

    // 获取方案下的所有小区
    getDistrictByVoteId: {
      url: 'xq/findSelectedCourt',
      method: 'GET',
      storage: false
    },

    // 获取方案图片
    getPlanImages: {
      url: 'xq/img/getFiles',
      method: 'GET'
    },

    // 获取方案问卷调查
    getQuestion: {
      url: 'wjxx/findAllOptionByPlan',
      method: 'GET'
    },

    // 提交问卷调查
    doVote: {
      url: 'vote/addvoteInfo',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      // 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response.success
        };
        if (ret.success) {
          ret.content = '投票成功';
        } else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }
    },

    // 根据业主id，方案id，小区id，查询投票结果
    getVoteResult: {
      url: 'vote/findOneByCourtAndPerson',
      method: 'GET'
    }

  }
);

context.create(
  'ywh', {

    // 获取候选人列表
    getCandidates: {
      url: 'rw/findBuddySignUp',
      method: 'GET'
    },

    // 获取用户所在小区
    getXiaoqu: {
      url: 'mp/loadCourtById',
      method: 'GET'
    },

    // 获取业委会投票任务
    getYwhTp: {
      url: 'rw/loadCourtActiveTask',
      method: 'GET'
    },

    // 业委会投票
    ywhTp: {
      url: 'mp/castunitadd',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      // 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response.success
        };
        if (ret.success) {
          ret.content = '投票成功';
        } else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }
    }
  }
);

export default context.api;
